<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | 王勇程 <laughskydragon@qq.com>                         |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\command;

use think\console\Command;
use think\console\Input;
use think\console\Output;

/**
 * VIP定时任务命令
 * 用于处理VIP相关的定时任务，如过期VIP处理、提醒发送、数据清理等
 * 
 * 使用方法：
 * php think vip:task expired    # 处理过期VIP用户
 * php think vip:task reminder   # 发送VIP过期提醒
 * php think vip:task cleanup    # 清理过期数据
 * php think vip:task all        # 执行所有任务
 * 
 * @author 王勇程 <laughskydragon@qq.com>
 */
class VipTask extends Command
{
    protected function configure()
    {
        $this->setName('vip:task')
            ->setDescription('VIP定时任务处理')
            ->addArgument('action', null, '任务类型: expired|reminder|cleanup|all');
    }

    protected function execute(Input $input, Output $output)
    {
        // 初始化OneBase常量（命令行模式下需要手动初始化）
        $this->initOneBaseConstants();
        
        $action = $input->getArgument('action');
        
        if (empty($action)) {
            $output->writeln('<error>请指定任务类型: expired|reminder|cleanup|all</error>');
            return 1;
        }

        try {
            // 获取Task逻辑层实例
            $taskLogic = app('app\common\logic\Task');
            
            switch ($action) {
                case 'expired':
                    $output->writeln('<info>开始处理过期VIP用户...</info>');
                    $result = $taskLogic->processExpiredVip();
                    break;
                    
                case 'reminder':
                    $output->writeln('<info>开始发送VIP过期提醒...</info>');
                    $result = $taskLogic->sendVipExpireReminder();
                    break;
                    
                case 'cleanup':
                    $output->writeln('<info>开始清理过期数据...</info>');
                    $result = $taskLogic->cleanupExpiredData();
                    break;
                    
                case 'all':
                    $output->writeln('<info>开始执行所有定时任务...</info>');
                    $results = [];
                    $results['expired'] = $taskLogic->processExpiredVip();
                    $results['reminder'] = $taskLogic->sendVipExpireReminder();
                    $results['cleanup'] = $taskLogic->cleanupExpiredData();
                    
                    foreach ($results as $task => $taskResult) {
                        if ($taskResult[0] == RESULT_SUCCESS) {
                            $output->writeln("<info>[$task] " . $taskResult[1] . "</info>");
                        } else {
                            $output->writeln("<error>[$task] " . $taskResult[1] . "</error>");
                        }
                    }
                    return 0;
                    
                default:
                    $output->writeln('<error>无效的任务类型，支持: expired|reminder|cleanup|all</error>');
                    return 1;
            }
            
            // 输出执行结果 - OneBase返回格式：[RESULT_SUCCESS/RESULT_ERROR, message, data]
            if ($result[0] == RESULT_SUCCESS) {
                $output->writeln('<info>' . $result[1] . '</info>');
                return 0;
            } else {
                $output->writeln('<error>' . $result[1] . '</error>');
                return 1;
            }
            
        } catch (\Exception $e) {
            $output->writeln('<error>任务执行失败：' . $e->getMessage() . '</error>');
            return 1;
        }
    }
    
    /**
     * 初始化OneBase常量
     * 命令行模式下AppInit事件可能未触发，需要手动初始化
     */
    private function initOneBaseConstants()
    {
        // 如果常量已定义则跳过
        if (defined('LAYER_MODEL_NAME')) {
            return;
        }
        
        // 手动触发InitBase行为
        $initBase = new \app\common\behavior\InitBase();
        $initBase->run();
    }
}